﻿using System;
using System.Collections.Generic;

namespace PureStateMachine
{
    public class PBlackboard
    {
        private Dictionary<string, object> _datas;

        public PBlackboard()
        {
            _datas = new Dictionary<string, object>();
        }
        
        public void RegisterData(string name, object obj)
        {
            if (_datas.ContainsKey(name))
            {
                throw new Exception($"注册失败，{name} 数据已存在");
            }
            else
            {
                _datas[name] = obj;
            }
        }

        public void RemoveData(string name)
        {
            if (_datas.ContainsKey(name))
            {
                _datas.Remove(name);
            }
            else
            {
                throw new Exception($"删除失败，{name} 数据不存在");
            }
        }

        public T GetData<T>(string name)
        {
            if (_datas.TryGetValue(name, out var obj))
            {
                if (obj is T o)
                {
                    return o;
                }
                else
                {
                    throw new Exception($"获取失败，{name} 数据类型不匹配，{name} 的类型是{obj.GetType()}，而请求的是{typeof(T)}");
                }
            }
            else
            {
                throw new Exception($"获取失败，{name} 数据不存在");
            }
        }
    }
}